package com.erhuogo.action;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.bean.Response;
import com.erhuogo.entity.Message;
import com.erhuogo.enumerate.Result;
import com.erhuogo.service.MessageService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 留言Action
 * @author aibinxiao
 * @date 2016年10月26日 上午11:33:06
 */
public class MessageAction extends ActionSupport {
	private static final long serialVersionUID = 4190976803741689698L;
	private static Logger logger = LoggerFactory.getLogger(MessageAction.class);
	
	// 模型驱动使用的对象
	private Message message;

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}
	
	// 注入MessageService
	private MessageService messageService;

	public MessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
	
	// 返回页面数据
	private Response response = new Response();

	public Response getResponse() {
		return response;
	}

	public void setResponse(Response response) {
		this.response = response;
	}
	
	/**
	 * 1.分页条件查询留言信息
	 * @return
	 */
	public String listPage(){
		String page = ServletActionContext.getRequest().getParameter("page");
		String rows = ServletActionContext.getRequest().getParameter("rows");
		String id = ServletActionContext.getRequest().getParameter("id");
		String commodityId = ServletActionContext.getRequest().getParameter("commodityId");
		String writerId = ServletActionContext.getRequest().getParameter("writerId");
		String readerId = ServletActionContext.getRequest().getParameter("readerId");
		String startDate = ServletActionContext.getRequest().getParameter("startDate");
		String endDate = ServletActionContext.getRequest().getParameter("endDate");
		
		GridPageBean<Message> listPage = messageService.findByPage(page, rows, id, commodityId, writerId ,readerId, startDate, endDate);
		if(listPage!=null && listPage.getRecords()>0){
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("查询成功");
		}else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("查询失败");
		}
		response.setData(listPage);
		return "listPageSUCCESS";
	}
	
	/**
	 * 2.根据id删除留言信息
	 * @return
	 */
	public String deleteById(){
		String id = ServletActionContext.getRequest().getParameter("id");
		try {
			messageService.deleteById(id);
			response.setMessage("删除成功");
		} catch (Exception e) {
			response.setMessage("删除失败");
			logger.info(e.getMessage());
			e.printStackTrace();
		}
		return "deleteSUCCESS";
	}
	
}
